home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 43 / Mac Magazin and MacEasy Magazine CD - Issue 43.iso / Software / Mobiles Büro / Newton / Newton Entwickler / DIL 2.0 Sample Code ƒ / CompNRun-1 / CompNRun.txt < prev    next >
Text File  |  1997-09-25  |  5KB  |  107 lines

  1. /*
  2. **      Newton Developer Technical Support Sample Code
  3. **
  4. **      CompNRun, a stream file used as a protocol extension by the PDILs
  5. **
  6. **      by David Fedor, Newton Developer Technical Support
  7. **
  8. **      Copyright © 1997 Apple Computer, Inc.  All rights reserved.
  9. **
  10. **      You may incorporate this sample code into your applications without
  11. **      restriction.  This sample code has been provided "AS IS" and the
  12. **      responsibility for its operation is 100% yours.  You are not
  13. **      permitted to modify and redistribute the source as "DTS Sample Code."
  14. **      If you are going to re-distribute the source, we require that you
  15. **      make it clear in the source that the code was descended from
  16. **      Apple-provided sample code, but that you've made changes.
  17. */
  18.  
  19. // The parameter is a frame:
  20. //    src:   The NewtonScript source which should be compiled
  21. //    args:  If specified, then the compiled src must be a function; it
  22. //           will be called with the specified arguments.  (If there are
  23. //           no arguments needed for your function, put NIL here.)
  24. //    debug: If this slot is non-NIL and something throws, CurrentException() 
  25. //           will be copied to element 1 of the returned array (see below).
  26. //
  27. // This protocol extension will return an array to the caller:
  28. //    element 0: either NIL (no problems), or an integer specifying
  29. //               what was being done when the throw happened.
  30. //               0=compiling, 1=evaluating, 2=calling the function
  31. //    element 1: the statement result, or the return value of calling the function
  32. //
  33. // This routine is intentionally compact & terse since it gets downloaded
  34. // to the Newton device during a PDIL session, and thus lives in the heap.
  35.  
  36. theStream := func(ep) begin
  37.     local param := ep:ReadCommandData();
  38.     local ret;
  39.     local obj;
  40.     local step:=0;    // used to tell where the throw happened
  41.  
  42.     try
  43.         obj:= compile(param.src);            // compile it
  44.         step:=1;
  45.         obj:=call obj with ();                // evaluate it
  46.         step:=2;
  47.         ret := [nil, (if param.args exists then call obj with (param.args) else obj)];
  48.     onexception |evt.ex| do begin
  49.         // tell the caller that a throw happened, and optionally what it was.
  50.         ret := [step, if param.debug then CurrentException()];
  51.     end;
  52.     ep:WriteCommand("CRun",ret,true);    // send the result to the desktop
  53. end;
  54.  
  55.  
  56. /*
  57. // And now for a useful little inspector trick, handy to convert the stream into
  58. // a good form for inserting into a C application as static data.  Edit the file
  59. // name and path at the bottom, select the whole block, and hit whatever key
  60. // you've set up to execute NewtonScript locally (see below for details).
  61. // Then copy the data out of the inspector window into your C source file!
  62. //
  63. // If you're using WinNTK, the stream file can be read directly.  With MacNTK,
  64. // it is more work since LoadDataFile() doesn't exist on MacNTK (yet).  You
  65. // should use something like Clipboard Magician or Res to get the stream file
  66. // converted into a resource, which can then be read with GetResource().
  67. // I put the data in a "STRM" resource inside the stream file; it could
  68. // be placed anywhere if the filename and resource information below is
  69. // changed appropriately.
  70. //
  71. // For WinNTK, to make shift-enter to execute NewtonScript locally,
  72. // make a file in NTK's directory called GlblData.f containing this code:
  73. //     protoeditor:DefineKey( {key:$\n, shift:true}, 'EvaluateSelection);
  74. // For MacNTK, do the same but name the file "GlobalData" (no quotes).
  75. // You must quit and restart NTK to have the definition take effect.
  76. // NOTE: $\n is the key above shift, not the one on the keypad.
  77. //
  78. // This will work similarly to the way you execute NewtonScript in the inspector
  79. // window, but it runs inside of NTK and a connection to a Newton device isn't
  80. // necessary.  You can evaluate NewtonScript code in any window, and the result
  81. // will be printed to the inspector window.
  82.  
  83.     call func(filename, rsrcType, rsrcID) begin
  84.         local f,data,i,t;
  85.         if rsrcType then begin
  86.             f:=OpenResFileX(filename);
  87.             data:=GetResource(rsrcType, rsrcID,'stream);
  88.             CloseResFileX(f);
  89.         end else
  90.             data:=LoadDataFile(filename, 'stream);    // we're on WinNTK so just read the data.
  91.       
  92.         write("{");
  93.         local hex:="0123456789ABCDEF";
  94.         for i:=0 to length(data)-1 do begin
  95.             t:=extractbyte(data,i);
  96.             if t<0 then t:=256+t;
  97.             write("0x" & hex[t div 16] & hex[t mod 16] & ", ");
  98.             if band(i,15)=15 then write("\n ");
  99.         end;
  100.         write("};\n");
  101.     // choose one of the following lines, depending on whether you're
  102.     // on WinNTK or MacNTK... and change the path as appropriate.
  103. //    end with ("c:\\CompNRun\\CompNRun.stm", nil, nil)
  104.     end with ("Innards:CompNRun:CompNRun.stream", "STRM", 128)
  105.  
  106. */
  107.